package slice

func ToMap[T comparable](src []T) map[T]struct{} {
	var dataMap = make(map[T]struct{}, len(src))
	for _, v := range src {
		if _, ok := dataMap[v]; !ok {
			dataMap[v] = struct{}{}
		}
	}
	return dataMap
}

// FilterMap 执行过滤并且转化
func FilterMap[T, E any](src []T, filter func(index int, value T) (E, bool)) []E {
	var res = make([]E, 0, len(src))
	for k, v := range src {
		dst, ok := filter(k, v)
		if ok {
			res = append(res, dst)
		}
	}
	return res
}

// ReduceMap 对元素进行一系列的操作并返回
func ReduceMap[T, E any](src []T, reduce func(index int, value T) E) []E {
	var res = make([]E, len(src))
	for k, val := range src {
		res[k] = reduce(k, val)
	}
	return res
}
